package com.zzh.service.impl;

import com.zzh.controller.Code;
import com.zzh.dao.BookDao;
import com.zzh.domain.Book;
import com.zzh.exception.BusinessException;
import com.zzh.exception.SystemException;
import com.zzh.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BookServiceImpl implements BookService {

    @Autowired
    private BookDao bookDao;

    public boolean save(Book book) {

        return bookDao.save(book) > 0;
    }

    public boolean update(Book book) {

        return bookDao.update(book) > 0;
    }

    public boolean delete(Integer id) {

        return bookDao.delete(id) > 0;
    }

    public Book getById(Integer id) {

        //模拟业务异常，包装成自定义异常
        if (id < 0) {
            throw new BusinessException(Code.BUSINESS_ERR, "书籍序号输入有误!");
        }

        return bookDao.getBookById(id);
    }

    public List<Book> getAll() {
        return bookDao.getAll();
    }


    public List<Book> getBooksByName(String name) {
        return bookDao.getBooksByName(name);
    }
}
